﻿using System;
using Songhay;
using Songhay.DataAccess.Models;
using Songhay.Models;

namespace Songhay.DataAccess.Models.Extensions
{

    /// <summary>
    /// Extensions of <see cref="IDocument"/>
    /// </summary>
    public static class IDocumentExtensions
    {
        /// <summary>
        /// Determines whether the specified document is template-able.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns>
        ///   <c>true</c> if the specified document is template-able; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsTemplatable(this IDocument document)
        {
            if (document == null) return false;
            if (string.IsNullOrEmpty(document.FileName)) return false;
            return document.FileName.EndsWith(".html");
        }

        /// <summary>
        /// Sets the defaults.
        /// </summary>
        /// <param name="document">The document.</param>
        public static void SetDefaults(this IDocument document)
        {
            if (document == null) return;

            document.CreateDate = DateTime.Now;
            document.IsActive = true;
            document.IsRoot = false;
            document.ModificationDate = DateTime.Now;
        }

        /// <summary>
        /// Converts the <see cref="IDocument" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="document">The document.</param>
        public static DisplayItemModel ToDisplayItemModel(this IDocument document)
        {
            return document.ToDisplayItemModel(DisplayItemModelCategories.GenericWebDocument);
        }

        /// <summary>
        /// Converts the <see cref="IDocument" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="category">The category.</param>
        public static DisplayItemModel ToDisplayItemModel(this IDocument document, string category)
        {
            if (document == null) return null;

            var data = new DisplayItemModel();
            data.DisplayText = document.Title;
            data.Id = document.DocumentId;
            data.ItemCategory = category;
            data.ItemName = document.DocumentShortName;

            return data;
        }

        /// <summary>
        /// Converts the <see cref="IDocument"/> into a display text.
        /// </summary>
        /// <param name="data">The data.</param>
        public static string ToDisplayText(this IDocument data)
        {
            if (data == null) return null;
            var s = string.Format("DocumentId: {0}, Title: {1}, IsActive: {2}", data.DocumentId, data.Title, data.IsActive);
            if (!string.IsNullOrEmpty(data.FileName)) s += string.Format(", Path:{0}, FileName: {1}", data.Path, data.FileName);
            if (!string.IsNullOrEmpty(data.DocumentShortName)) s += string.Format(", DocumentShortName: {0}", data.DocumentShortName);
            return s;
        }

        /// <summary>
        /// Converts the <see cref="IDocument"/> into a menu display item model.
        /// </summary>
        /// <param name="document">The document.</param>
        public static MenuDisplayItemModel ToMenuDisplayItemModel(this IDocument document)
        {
            return document.ToMenuDisplayItemModel(DisplayItemModelCategories.GenericWebDocument);
        }

        /// <summary>
        /// Converts the <see cref="IDocument"/> into a menu display item model.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="category">The category.</param>
        public static MenuDisplayItemModel ToMenuDisplayItemModel(this IDocument document, string category)
        {
            if (document == null) return null;

            var item = document.ToDisplayItemModel(category);
            var menuItem = new MenuDisplayItemModel();
            FrameworkTypeUtility.SetProperties(item, menuItem);
            return menuItem;
        }

        /// <summary>
        /// Updates the specified document.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="data">The data.</param>
        public static void Update(this IDocument document, IDocument data)
        {
            if (document == null) return;
            if (data == null) return;

            FrameworkTypeUtility.SetProperties(document, data);
        }
    }
}
